<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../js/jquery-3.6.0.js"></script>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        h3 {
            padding: 20px 10px;
        }

        li {
            list-style: none;
        }

        .list li {
            line-height: 45px;
            text-indent: 10px;
            border: 1px solid #333;
        }

        .buttonGroup {
            margin-top: 20px;
        }

        .active {
            background-color: #6ff;
        }

        .buttonGroups {
            padding: 10px;
        }
    </style>
</head>

<body>
    <div id="demo" class="wrap">
        <div class="buttonGroups">
            <button>替换</button>
        </div>
        <ul class="list">
            <li class="first">01</li>
            <li>02</li>
            <li class="mid">03</li>
            <li>04</li>
            <li>05</li>
            <li class="last"><span>06</span></li>
        </ul>
    </div>
</body>
<script>
    // 替换的语法
    // $(oldEle).replaceWith(newEle)   // 将匹配的元素 替换为 另一个元素(新的 已存在的)   (将原本的元素替换为新的元素)
    // $(newEle).replaceAll(oldEle)   // 用匹配的元素 替换 另一个元素  (用新的元素替换所有已存在的元素)

    $("button").eq(0).click(function () {
        /* var $li = $("<li/>", {
            text: "123123",
            // html: "<span>asdasd</span>",
            class: "newLi",
            width: 200,
            height: 200,
            css: {
                textAlign: "center",
                lineHeight: "200px",
            },
            click: function () {
                console.log($(this).index());
            }
        })
        $(".mid").replaceWith($li); */


        $("<li/>", {
            text: "123123",
            // html: "<span>asdasd</span>",
            class: "newLi",
            width: 200,
            height: 200,
            css: {
                textAlign: "center",
                lineHeight: "200px",
            },
            click: function () {
                console.log($(this).index());
            }
        }).replaceAll(".mid");

    })



</script>

</html>